
from flask import Flask, Blueprint, render_template, session, redirect, url_for, request, Response, jsonify
# 这里注意要导入models中的models，而不是extends中的models
from App.models import *

# 申明一个蓝图对象user
accountblue = Blueprint('accountblue', __name__)

# 显示登录界面
@accountblue.route('/', methods=['GET', 'POST'])
def viewLogin():
    # list = getAllAccount()
    # list_dict = [dict(model) for model in list]
    # print('1')
    # return jsonify(list_dict)
    return render_template('page-login.html')


@accountblue.route('/account/login', methods=['GET', 'POST'])
def login():
    print('login')

    username = request.form.get('username')
    password = request.form.get('password')
    result = Account.query.filter(Account.userid == username, Account.password == password).first()
    print(result)
    # 登录成功或者失败的判断
    if result:
        print('login successfully')
        # 定义一个字典
        userItem = {}
        # 开始存数据
        userItem['userid'] = result.userid
        userItem['password'] = result.password
        session['user'] = userItem
        print('here')
        return render_template('index.html')

    else:
        print('login error')
        return render_template('page-login.html', message='用户名不存在或密码错误！')


@accountblue.route('/test', methods=['GET', 'POST'])
def test():
    return render_template('index.html')


@accountblue.route('/sendAjax')
def send():
    print("sendAjax")
    accounts = getAllAccount()
    accounts_dict = [dict(model) for model in accounts]
    print(accounts[2].password)
    print(accounts)
    return jsonify(accounts_dict)
